package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mall.dao.SkuSaleAttrValueDao;
import com.mall.dao.SpuInfoDao;
import com.mall.entity.*;
import com.mall.handler.AllExceptionHandler;
import com.mall.service.*;
import com.mall.vo.AttrsVo;
import com.mall.vo.PurchaseVo;
import com.mall.vo.SkuVo;
import com.mall.vo.WebResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.dao.SkuInfoDao;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfo> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private SpuInfoDao spuInfoDao;

    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;


    @Autowired
    private SkuInfoDao skuInfoDao;

    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        this.baseMapper.insert(skuInfo);
    }

    @Override
    public void saveSku(SkuVo vos) {
        //保存sku的基本信息
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setSpuId(vos.getSpuId());
        skuInfo.setSkuName(vos.getSkuName());
        skuInfo.setBrandId(vos.getBrandId());
        skuInfo.setSkuDefaultImg(vos.getImg());
        skuInfo.setSkuTitle(vos.getDescp());
        skuInfo.setSkuTitle(vos.getDescp());
        skuInfo.setPrice(vos.getPrice());
        skuInfo.setSkuStock(vos.getSkuStock());
        this.baseMapper.insert(skuInfo);

      //  List<SpuInfo> spu_id = spuInfoService.list(new QueryWrapper<SpuInfo>().eq("id", vos.getSpuId()));
        SpuInfo info = spuInfoDao.selectOne(new QueryWrapper<SpuInfo>().eq("id", skuInfo.getSpuId()));
        BigDecimal bigDecimal= new BigDecimal(String.valueOf(info.getMinPrice()));
//        for (SpuInfo spuInfo : spu_id) {
//            bigDecimal = new BigDecimal(String.valueOf(spuInfo.getMinPrice()));
//        }
        BigDecimal price = vos.getPrice();
        if (price.compareTo(bigDecimal)<=0){
            SpuInfo spuInfo = new SpuInfo();
            spuInfo.setMinPrice(price);
            spuInfoService.update(spuInfo,new UpdateWrapper<SpuInfo>().eq("id",skuInfo.getSpuId()));
        }

        //保存图片信息
        List<String> images = vos.getImages();
        List<SkuImages> skuImages = new ArrayList<>();
        for (String image : images) {
            SkuImages skuImage = new SkuImages();
            skuImage.setSkuId(skuInfo.getSkuId());
            skuImage.setImgUrl(image);
            skuImage.setImgSort(0);
            if (image.equals(vos.getImg())){
                skuImage.setDefaultImg(1);
            }else {
                skuImage.setDefaultImg(0);
            }
            skuImages.add(skuImage);
        }
        skuImagesService.saveBatch(skuImages);
    }

    @Override
    public void saveAllAttr(String groupName, Long id, AttrsVo[] attrsVos) {
        //根据sku_id获取spu_id 在根据spu_id获取catlog_id
        SkuInfo skuInfo = this.baseMapper.selectById(id);
        Long spuId = skuInfo.getSpuId();

        SpuInfo spu = spuInfoDao.selectOne(new QueryWrapper<SpuInfo>().eq("id", spuId));
        Long catId=spu.getCatId();


        AttrGroup attrGroup = new AttrGroup();
        attrGroup.setAttrGroupName(groupName);
        attrGroup.setSort(0);
        attrGroup.setDescript(groupName);
        attrGroup.setCategoryId(catId);
        attrGroupService.save(attrGroup);

        //获取到分组id
        Long attrGroupId = attrGroup.getAttrGroupId();

        for (AttrsVo attrsVo : attrsVos) {
            String key = attrsVo.getKey();
            String value = attrsVo.getValue();

            Attr attr = new Attr();
            attr.setAttrName(key);
            attr.setValueSelect(value);
            attr.setAttrType(0);
            attr.setShowDesc(0);
            attr.setValueType(0);
            attr.setSearchType(0);
            attr.setCategoryId(catId);
            attr.setStatus(1L);
            attrService.save(attr);


            SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
            skuSaleAttrValue.setSkuId(id);
            skuSaleAttrValue.setAttrId(attr.getAttrId());
            skuSaleAttrValue.setAttrName(key);
            skuSaleAttrValue.setAttrValue(value);
            skuSaleAttrValueService.save(skuSaleAttrValue);

            AttrAttrgroupRelation relation = new AttrAttrgroupRelation();
            relation.setAttrId(attr.getAttrId());
            relation.setAttrGroupId(attrGroupId);
            attrAttrgroupRelationService.save(relation);
        }
        SpuInfo spuInfo = new SpuInfo();
        spuInfo.setShowStatus(1);
        spuInfo.setBrandId(skuInfo.getBrandId());
        spuInfoService.update(spuInfo,new UpdateWrapper<SpuInfo>());
        //设置attr 将其中的type置为0   catlog_id
        //在设置sku_sale_attr_value
        //在spu表中将展示状态改为1
    }

    @Override
    public List<SkuInfo> selectPrice(Long[] attrIds, Long spuId) {
        Set<Long>set=new HashSet<>();
        Long skuId = null;
        for (Long attrId : attrIds) {
            SkuSaleAttrValue skuSaleAttrValue = skuSaleAttrValueDao.selectOne(new QueryWrapper<SkuSaleAttrValue>().eq("id", attrId));
            skuId = skuSaleAttrValue.getSkuId();
            set.add(skuId);
        }
        if (set.size() != 1){
            return null;
        }
        List<SkuInfo>skuInfoList=new ArrayList<>();
        SkuInfo skuInfo = skuInfoDao.selectOne(new QueryWrapper<SkuInfo>().eq("sku_id", skuId));
        skuInfoList.add(skuInfo);
        return skuInfoList;
    }

    @Override
    public PurchaseVo selectPriceAndStock(Long[] attrIds, Long spuId) {
        Set<Long>set=new HashSet<>();
        Long skuId = null;
        for (Long attrId : attrIds) {
            SkuSaleAttrValue skuSaleAttrValue = skuSaleAttrValueDao.selectOne(new QueryWrapper<SkuSaleAttrValue>().eq("id", attrId));
            skuId = skuSaleAttrValue.getSkuId();
            set.add(skuId);
        }
        if (set.size() != 1){
            return null;
        }
        SkuInfo skuInfo = skuInfoDao.selectOne(new QueryWrapper<SkuInfo>().eq("sku_id", skuId));
        PurchaseVo purchaseVo=new PurchaseVo();
        BeanUtils.copyProperties(skuInfo,purchaseVo);

        List<SkuImages> sku_id = skuImagesService.list(new QueryWrapper<SkuImages>().eq("sku_id", skuInfo.getSkuId()));
        List<String>image=new ArrayList<>();
        for (SkuImages skuImages : sku_id) {
            String imgUrl = skuImages.getImgUrl();
            image.add(imgUrl);
        }
        purchaseVo.setImages(image);

        return purchaseVo;
    }



}